﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;

using Newtonsoft.Json;

namespace Stock.Util
{
    public static class StringUtil
    {
        private static List<string> weekList;

        static StringUtil()
        {
            weekList = new List<string>
            {
                "周日",
                "周一",
                "周二",
                "周三",
                "周四",
                "周五",
                "周六"
            };
        }

        public static T Parse<T>(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return default;
            }
            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return default;
            }
        }

        public static string ToJson(object obj)
        {
            JsonSerializerSettings jss = new JsonSerializerSettings
            {
                Formatting = Formatting.Indented
            };
            return JsonConvert.SerializeObject(obj, jss);
        }

        public static double ToDouble(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return 0;
            }
            if (double.TryParse(s, out double d))
                return d;
            return 0;
        }

        public static double ToFixed(string s, int digits = 2)
        {
            double d = ToDouble(s);
            return ToFixed(d, digits);
        }

        public static double ToFixed(double f, int digits = 2)
        {
            return Math.Round(f, digits, MidpointRounding.AwayFromZero);
        }

        public static string ToEyeString(double d, bool eyeOpend, bool isTotal = false)
        {
            if (eyeOpend)
            {
                if (d == 0 && !isTotal)
                {
                    return "--";
                }
                return d.ToString();
            }

            if (d == 0)
            {
                return "--";
            }

            if (d < 0)
            {
                d *= -1;
            }

            return ToEyeString(d.ToString(), eyeOpend);
        }

        public static string ToEyeString(string d, bool eyeOpend)
        {
            if (eyeOpend)
            {
                return d;
            }
            return Regex.Replace(d, @"[\d]", "*").Replace("%", "");
        }

        public static string GetDayOfWeekString(int? dayOfWeek = null)
        {
            int day = dayOfWeek ?? (int)DateTime.Now.DayOfWeek;
            return weekList[day];
        }

        public static long GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return (long)ts.TotalMilliseconds;
        }

        public static string GetVersionString(Type type)
        {
            return GetVersion(type).ToString();
        }

        public static Version GetVersion(Type type)
        {
            return type.Assembly.GetName().Version;
        }

        public static Version GetVersion(string versionString)
        {
            if (string.IsNullOrEmpty(versionString))
            {
                versionString = "";
            }
            versionString = versionString.Replace("v", "").Replace("V", "");

            if (Version.TryParse(versionString, out Version version))
            {
                return version;
            }
            return new Version(1, 0, 0, 0);
        }

        public static bool IsEmpty(IList list)
        {
            return list == null || list.Count < 1;
        }

        public static bool IsNotEmpty<T>(ICollection<T> list)
        {
            return list != null && list.Count > 0;
        }
    }
}
